package com.example.treehole;

import android.graphics.Color;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.baidu.location.BDAbstractLocationListener;
import com.baidu.location.BDLocation;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.example.treehole.base.BaseBindingActivity;
import com.example.treehole.bean.Chat;
import com.example.treehole.databinding.ActivityMainBinding;
import com.example.treehole.fragment.CityFragment;
import com.example.treehole.fragment.CourseScheduleFragment;
import com.example.treehole.fragment.MessageListFragment;
import com.example.treehole.fragment.MineFragment;
import com.example.treehole.fragment.PostFragment;
import com.example.treehole.util.Api;
import com.example.treehole.util.HttpUtil;
import com.google.android.material.badge.BadgeDrawable;
import com.google.android.material.navigation.NavigationBarView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class MainActivity extends BaseBindingActivity<ActivityMainBinding> {


    @Override
    protected void initListener() {

    }

    LocationClient mLocationClient;
    MyLocationListener myLocationListener;

    public class MyLocationListener extends BDAbstractLocationListener {
        @Override
        public void onReceiveLocation(BDLocation location) {
            if (location == null) {
                return;
            }
            HashMap<String, Object> params = new HashMap<>();
            params.put("latitude", location.getLatitude());
            params.put("longitude", location.getLongitude());
            params.put("userid", App.user.id);
            HttpUtil.post(Api.LOVE_ADD_LOCATION, params, new HttpUtil.OnNetStringListener() {
                @Override
                public void success(String data, String message) {

                }

                @Override
                public void error(String message) {

                }
            });
            mLocationClient.stop();
            Log.d("TAG", "onReceiveLocation: " + location.getAddrStr());

        }
    }

    private void requestLocation() {
        mLocationClient = new LocationClient(getApplicationContext());
        LocationClientOption locationOption = new LocationClientOption();
        myLocationListener = new MyLocationListener();
        mLocationClient.registerLocationListener(myLocationListener);
        locationOption.setLocationMode(LocationClientOption.LocationMode.Device_Sensors);
        locationOption.setCoorType("bd09ll");
        locationOption.setScanSpan(1000);
        locationOption.setIsNeedAddress(true);
        locationOption.setIsNeedLocationDescribe(true);
        locationOption.setNeedDeviceDirect(false);
        locationOption.setLocationNotify(true);
        locationOption.setIgnoreKillProcess(true);
        locationOption.setIsNeedLocationDescribe(true);
        locationOption.setIsNeedLocationPoiList(true);
        locationOption.SetIgnoreCacheException(false);
        locationOption.setOpenGps(true);
        locationOption.setIsNeedAltitude(false);
        locationOption.setOpenAutoNotifyMode();
        locationOption.setOpenAutoNotifyMode(3000, 1, LocationClientOption.LOC_SENSITIVITY_HIGHT);
        mLocationClient.setLocOption(locationOption);
        mLocationClient.start();
    }

    @Override
    protected void initData() {
        if (!App.isLogin()) {
            startActivity(LoginActivity.class);
            finish();
            return;
        }

        requestLocation();
        fragments.add(new PostFragment());
        fragments.add(new MessageListFragment());
        fragments.add(new CourseScheduleFragment());
        fragments.add(new CityFragment());
        fragments.add(new MineFragment());
        viewBinder.bnv.inflateMenu(R.menu.main_bottom);

        changePage(fragments.get(0));
        viewBinder.bnv.setItemIconTintList(null);
        viewBinder.bnv.setOnItemSelectedListener(new NavigationBarView.OnItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                switch (item.getItemId()) {
                    case R.id.post:
                        changePage(fragments.get(0));
                        break;
                    case R.id.message:
                        changePage(fragments.get(1));
                        break;
                    case R.id.course:
                        changePage(fragments.get(2));
                        break;
                    case R.id.weather:
                        changePage(fragments.get(3));
                        break;
                    case R.id.mine:
                        changePage(fragments.get(4));
                        break;
                }
                return true;
            }
        });
        handler.sendEmptyMessageDelayed(1, 2000);
    }

    private Handler handler = new Handler(Looper.getMainLooper()) {
        @Override
        public void handleMessage(@NonNull android.os.Message msg) {
            super.handleMessage(msg);
            if (isDestroyed() || isFinishing()) {
                return;
            }
//            refresh();
        }
    };

    private void refresh() {
        HashMap<String, Object> params = new HashMap<>();
        params.put("userid", App.user.id);
        HttpUtil.getList(Api.CHAT_GET, params, Chat.class, new HttpUtil.OnNetListListener<Chat>() {
            @Override
            public void success(List<Chat> data, String message) {
                Menu menu = viewBinder.bnv.getMenu();
                MenuItem menuItem = menu.getItem(1);
                BadgeDrawable badgeDrawable = viewBinder.bnv.getOrCreateBadge(menuItem.getItemId());
                int total = 0;
                for (Chat datum : data) {
                    total += datum.unread;
                }
                if (total == 0) {
                    badgeDrawable.setNumber(total);
                    badgeDrawable.setVisible(false);
                } else {
                    badgeDrawable.setNumber(total);
                    badgeDrawable.setVisible(true);
                }
                handler.sendEmptyMessageDelayed(1, 2000);
            }

            @Override
            public void error(String message) {
                toast(message);
            }
        });
    }

    private List<Fragment> fragments = new ArrayList<>();

    private void changePage(Fragment fragment) {
        FragmentManager supportFragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = supportFragmentManager.beginTransaction();
        for (Fragment item : fragments) {
            fragmentTransaction.hide(item);
        }
        if (fragment.isAdded()) {
            fragmentTransaction.show(fragment);
        } else {
            fragmentTransaction.add(R.id.fcv, fragment).show(fragment);
        }
        fragmentTransaction.commitAllowingStateLoss();
    }


}